What is @turf/convex?
@turf/convex is a module in the Turf.js library that allows you to compute the convex hull of a set of points. A convex hull is the smallest convex polygon that can enclose all the points in a given dataset. This is useful in various geospatial analyses, such as determining the boundary of a set of geographical points.
What are @turf/convex's main functionalities?
Compute Convex Hull
This feature allows you to compute the convex hull of a set of points. The code sample demonstrates how to create a collection of points and then compute the convex hull using the @turf/convex module.
const turf = require('@turf/turf');
const points = turf.featureCollection([
turf.point([10, 10]),
turf.point([20, 20]),
turf.point([30, 30]),
turf.point([40, 40]),
turf.point([50, 50])
]);
const convexHull = turf.convex(points);
console.log(convexHull);
Other packages similar to @turf/convex
hull.js
hull.js is a JavaScript library for computing the convex hull of a set of 2D points. It is lightweight and easy to use, but it focuses solely on convex hull calculations, whereas @turf/convex is part of the larger Turf.js library, which offers a wide range of geospatial analysis tools.
concaveman
concaveman is a fast 2D concave hull algorithm. While it focuses on concave hulls, it can also be used to compute convex hulls by setting the concavity parameter to 1. It is faster and more flexible for certain use cases compared to @turf/convex, but it is not as feature-rich in terms of geospatial analysis.
@turf/convex
convex
Takes a Feature or a FeatureCollection and returns a convex hull Polygon.
Internally this uses
the convex-hull module that
implements a monotone chain hull.
Parameters
Examples
var points = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.195312, 43.755225]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.404052, 43.8424511]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.579833, 43.659924]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.360107, 43.516688]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.14038, 43.588348]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [10.195312, 43.755225]
}
}
]
};
var hull = turf.convex(points);
var resultFeatures = points.features.concat(hull);
var result = {
"type": "FeatureCollection",
"features": resultFeatures
};
Returns Feature<Polygon> a convex hull
This module is part of the Turfjs project, an open source
module collection dedicated to geographic algorithms. It is maintained in the
Turfjs/turf repository, where you can create
PRs and issues.
Installation
Install this module individually:
$ npm install @turf/convex
Or install the Turf module that includes it as a function:
$ npm install @turf/turf